package team;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * This class basically constructs a HashMap to store the binding relationship between Auto Makers' full name and Makers' Table Name
 * @author jerry
 *
 */
public class AutoMaker {
	protected final static HashMap<String, String> makerTable = new HashMap<String, String>();
	
	private AutoMaker() {}
	
	/**
	 * Retrieve Auto Maker Data from Auto Table, and put those data into a HashMap
	 */
	public static void createMakerTable() {
		makerTable.clear();
		try {
			DBOperation dbop = new DBOperation();
			ResultSet rs = dbop.createMakerTable();
			while (rs.next()) {
				AutoMaker.makerTable.put(rs.getString(1), rs.getString(2));
			}
			dbop.disconnectFromDB();
		} catch (SQLException e) {
			System.err.println("Error in AutoMaker.createMakerTable" + e.getMessage());
		}
	}

	/**
	 * Get a Array of the makers' full name
	 * @return array of auto makers' full name
	 */
	public static String[] getMakerName() {
		Set<String> makerSet = makerTable.keySet();
		ArrayList<String> maker = new ArrayList<String>();
		Iterator<String> i = makerSet.iterator();
		while (i.hasNext()) {
			maker.add(i.next().toString());
		}
		Collections.sort(maker);
		return maker.toArray(new String[maker.size()]);
	}
	
	/**
	 * Add a new Maker table
	 * @param makerName
	 * @param tableName
	 * @return
	 */
	public static boolean addMaker(String makerName, String tableName) {
		boolean added = false;
		try {
			DBOperation dbop = new DBOperation();
			dbop.addMakerToAutoTable(makerName, tableName);
			createMakerTable();
		} catch (SQLException e) {
			System.err.println("Error in AutoMaker.addMaker: " + e.getMessage());
		}
		return added;
	}
}
